import { Random } from "mockjs" // 导出随机函数

function loginAPI(req) {
  // req是一个请求对象，包含: url，type和body等属性
  const url = req.url.split('?')[1]
  const username = url.split('&')[0].split('=')[1]
  const password = url.split('&')[1].split('=')[1]
  let token = ''
  let message = '登录成功'
  let code = 200
  if (username === 'admin') {
    token = 'admin-' + Random.guid()
  } else if (username === 'merchant') {
    token = 'merchant-' + Random.guid()
  } else if (username === 'staff'){
    token = 'staff-' + Random.guid()
  } else {
    code = 401
    token = ''
    message = '账号不存在'
  }
  const data = {
    code: code, // 响应状态码
    data: {
      username: Random.cname(),
      token: token 
    },
    message: message
  }
  return data
}

export default {
  loginAPI,
}

